'use strict';

console.log(`第一题`);
//+ 练习：利用reduce()求积
function product(arr){
    return arr.reduce(function(x,y){
        return x * y;
    });
}

console.log(product([1,2,3]));

console.log(`第二题`);
//练习：不要使用JavaScript内置的parseInt()函数，利用map和reduce操作实现一个string2int()函数：
function string2int(s){
    return Array.from(s).map((x)=>x-'0').reduce(function(x,y){
        return x *10 +y;
    });
}
console.log(string2int);

console.log(`第三题`);
//回调函数
function get_primes(arr){
    return arr.filter(function(x){
        var a = parseInt(Math.sqrt(x));
        var b = false;
        for(var i =2;i<=a;i++){
            if(x % i == 0){
                b = true;
                break;
            }
            if(!b && x >1){
                return x;
            }
        }
    })
}

console.log(`第四题`);
var arr = [1,2];
arr.unshift('A','B');
console.log(arr);


var arr1 = [1,23];
arr1.push('A','NB');
console.log(arr1);

console.log(`第五题`);
var arr2 = [1,2];
arr2.unshift('怎么说','这么说');
console.log(arr2);
console.log(arr2.shift());
console.log(arr2);

var arr3 = [2,3];
arr3.push('这么帅','詹姆斯');
console.log(arr3);
console.log(arr3.pop());
console.log(arr3);

console.log(`第六题`);
var arr4 = ['c','b','a','f','e'];
console.log(arr4.sort());


var arr5 = ['大黄','敦煌','笔画','壁画'];
console.log(arr5.reverse());
console.log(arr5);

console.log(`第七题`);
var sum = [1,2,3,'get','num'];
console.log(arr.length);


console.log(`第八题`);
var nk = [[1,2,3],[400,500,600]];
var h = nk[1].slice(1,2);
console.log(h);

var hhh = [[1,24,65],[400,500,600]]
var f = hhh[1][1];
console.log(f);


console.log(`第九题`);
var ff = [10,20,40,30,6];
ff.sort(function(x,y){
    if(x<y){
        return -1;
    }
    if(x>y){
        return 1
    }
    return 0;
})
console.log(ff);

console.log(`第10题`);
function func(n){
    if(n>=1){
        let mult =1;
        for(let i =1;i<=n;i++){
            mult *=i;
        }
        return mult;
    }
}


console.log(func(10));

function ar(x){
    if(x>=1){
        var get =1;
        for(var i=1;i<=x;i++){
            get *= i;
        }
        return get;
    }
}
console.log(ar(10));

function fff(b){
    if(b>=1){
        var get =1;
        for(var i =1;i<=b;i++){
            get *=i;
        }
        return get;
    }
}
console.log(fff(10));
console.log(`第九题`);

function func1(str){
    let strTrim = str.trim();
    console.log(strTrim.length);
}

func1('  ddwad   ')

console.log(`第九题`);
console.log(`第九题`);
console.log(`第九题`);